﻿using CURE.Common;
using CUREBusinessLogicLayer.Harmful;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CURE.Handler
{
    /// <summary>
    /// ApprovalHandler 的摘要说明
    /// </summary>
    public class ApprovalHandler : IHttpHandler
    {

        #region 变量
        private string mid;

        public string Mid
        {
            get { return mid; }
            set { mid = value; }
        }
        private string fid;

        public string Fid
        {
            get { return fid; }
            set { fid = value; }
        }
        private string approval;

        public string Approval
        {
            get { return approval; }
            set { approval = value; }
        }
        private string reason;

        public string Reason
        {
            get { return reason; }
            set { reason = value; }
        }
        private string action;

        public string Action
        {
            get { return action; }
            set { action = value; }
        }

        private string page;

        public string Page
        {
            get { return page; }
            set { page = value; }
        }

        #endregion

        private LedgerBll ledgerbll = new LedgerBll();
        

        public void ProcessRequest(HttpContext context)
        {
            Mid = context.Request.Form["id"];
            Fid = context.Request.Form["fid"];
            Approval = context.Request.Form["approval"];
            Reason = context.Request.Form["txtReason"];
            Action = context.Request.Form["action"];
            Page = context.Request.Form["page"];
            string state = null;
            string approvaltype = null;
            
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("id", Mid);
            dic.Add("reason", Reason);
            dic.Add("approval", Approval);
            dic.Add("fid", Fid);
            
            switch (Action)
            {
                case "make": approvaltype = "0"; 
                    if (Approval == "0")
                    {
                        state = "2";
                    }
                    else
                    {
                        state = "-2";
                    }
                    break;
                default: break;
            }
            dic.Add("state", state);
            dic.Add("value", approvaltype);

            if (ApprovalRoutine(context, dic))
            {

                context.Response.Write(success("Harmful/" + page + ".aspx"));
            }
            else
            {
                context.Response.Write(failure());
            }

        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="context"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        private bool ApprovalRoutine(HttpContext context, Dictionary<string, object> dic)
        {
            return ledgerbll.ApprovalRoutine(dic);
        }

        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"navTabId\":\"editLedger\", \"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string successfoward(string str, string url)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"forward\",\"forwardUrl\":\"" + url + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}